import { defineStore } from "pinia";
const todos = defineStore('todos', {
    state: () => ({
        list: [
            {
                id: 1,
                name: '吃饭',
                done: false,
            },
            {
                id: 2,
                name: '睡觉',
                done: false,
            },
            {
                id: 3,
                name: '打豆豆',
                done: false,
            },
        ],
        filters: ['所有', '当前进行中', '已完成'],
        active: 'All',
    }),
    actions: {
        changeDone(id) {
            const todo = this.list.find((item) => item.id === id)
            todo.done = !todo.done
        },
        delCounter(id) {
            this.list = this.list.filter((item) => item.id !== id)
        },
        checkAll(value) {
            this.list.forEach((item) => (item.done = value))
        },
        addTodo(name) {
            this.list.unshift({
                id: Date.now(),
                name,
                done: false,
            })
        },
        clearCompleted() {
            this.list = this.list.filter((item) => !item.done)
        },

        changeActive(active) {
            this.active = active
        },
        showList() {
            if (this.active === '当前进行中') {
                return this.list.filter((item) => !item.done)
            } else if (this.active === '已完成') {
                return this.list.filter((item) => item.done)
            } else {
                return this.list
            }
        },
    },
    getters: {
        isCheckAll() {
            return this.list.every((item) => item.done)
        },
        leftCount() {
            return this.list.filter((item) => !item.done).length
        },
    }
})

export default todos;